home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 83 / MacAddict_083_2003-07.iso / mac / Software / Development / VLC Source 0.5.3.dmg / src / misc / modules.c < prev    next >
C/C++ Source or Header  |  2003-04-07  |  34KB  |  1,047 lines

  1. /*****************************************************************************
  2.  * modules.c : Builtin and plugin modules management functions
  3.  *****************************************************************************
  4.  * Copyright (C) 2001 VideoLAN
  5.  * $Id: modules.c,v 1.117 2003/03/25 15:38:14 gbazin Exp $
  6.  *
  7.  * Authors: Samuel Hocevar <sam@zoy.org>
  8.  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
  9.  *          Hans-Peter Jansen <hpj@urpla.net>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  24.  *****************************************************************************/
  25.  
  26. /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
  27.  * is set to 64. Don't try to be cleverer. */
  28. #ifdef _FILE_OFFSET_BITS
  29. #undef _FILE_OFFSET_BITS
  30. #endif
  31.  
  32. #include <stdlib.h>                                      /* free(), strtol() */
  33. #include <stdio.h>                                              /* sprintf() */
  34. #include <string.h>                                              /* strdup() */
  35.  
  36. #include <vlc/vlc.h>
  37.  
  38. #ifdef HAVE_DIRENT_H
  39. #   include <dirent.h>
  40. #elif defined( UNDER_CE )
  41. #   include <windows.h>                               /* GetFileAttributes() */
  42. #else
  43. #   include "../extras/dirent.h"
  44. #endif
  45.  
  46. #ifdef HAVE_SYS_TYPES_H
  47. #   include <sys/types.h>
  48. #endif
  49. #ifdef HAVE_SYS_STAT_H
  50. #   include <sys/stat.h>
  51. #endif
  52. #ifdef HAVE_UNISTD_H
  53. #   include <unistd.h>
  54. #endif
  55.  
  56. #if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
  57. #   include <dlfcn.h>                        /* dlopen(), dlsym(), dlclose() */
  58. #   define HAVE_DYNAMIC_PLUGINS
  59. #elif defined(HAVE_IMAGE_H)                                          /* BeOS */
  60. #   include <image.h>
  61. #   define HAVE_DYNAMIC_PLUGINS
  62. #elif defined(UNDER_CE)
  63. #   define HAVE_DYNAMIC_PLUGINS
  64. #elif defined(WIN32)
  65. #   define HAVE_DYNAMIC_PLUGINS
  66. #else
  67. #   undef HAVE_DYNAMIC_PLUGINS
  68. #endif
  69.  
  70. #include "error.h"
  71. #include "netutils.h"
  72.  
  73. #include "interface.h"
  74. #include "vlc_playlist.h"
  75. #include "intf_eject.h"
  76.  
  77. #include "stream_control.h"
  78. #include "input_ext-intf.h"
  79. #include "input_ext-dec.h"
  80. #include "input_ext-plugins.h"
  81.  
  82. #include "video.h"
  83. #include "video_output.h"
  84.  
  85. #include "audio_output.h"
  86. #include "aout_internal.h"
  87.  
  88. #include "stream_output.h"
  89.  
  90. #include "iso_lang.h"
  91.  
  92. #if defined( UNDER_CE )
  93. #   define MYCHAR wchar_t
  94. #else
  95. #   define MYCHAR char
  96. #endif
  97.  
  98. #ifdef HAVE_DYNAMIC_PLUGINS
  99. #   include "modules_plugin.h"
  100. #endif
  101.  
  102. #if defined( UNDER_CE )
  103. #    include "modules_builtin_evc.h"
  104. #elif defined( _MSC_VER )
  105. #    include "modules_builtin_msvc.h"
  106. #else
  107. #    include "modules_builtin.h"
  108. #endif
  109.  
  110. /*****************************************************************************
  111.  * Local prototypes
  112.  *****************************************************************************/
  113. #ifdef HAVE_DYNAMIC_PLUGINS
  114. static void AllocateAllPlugins   ( vlc_object_t * );
  115. static void AllocatePluginDir    ( vlc_object_t *, const MYCHAR *, int );
  116. static int  AllocatePluginFile   ( vlc_object_t *, MYCHAR * );
  117. #endif
  118. static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
  119. static int  DeleteModule ( module_t * );
  120. #ifdef HAVE_DYNAMIC_PLUGINS
  121. static void DupModule    ( module_t * );
  122. static void UndupModule  ( module_t * );
  123. static int  CallEntry    ( module_t * );
  124. #endif
  125.  
  126. /*****************************************************************************
  127.  * module_InitBank: create the module bank.
  128.  *****************************************************************************
  129.  * This function creates a module bank structure which will be filled later
  130.  * on with all the modules found.
  131.  *****************************************************************************/
  132. void __module_InitBank( vlc_object_t *p_this )
  133. {
  134.     module_bank_t *p_bank;
  135.  
  136.     p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
  137.     p_bank->psz_object_name = "module bank";
  138.  
  139.     /*
  140.      * Store the symbols to be exported
  141.      */
  142. #ifdef HAVE_DYNAMIC_PLUGINS
  143.     STORE_SYMBOLS( &p_bank->symbols );
  144. #endif
  145.  
  146.     /* Everything worked, attach the object */
  147.     p_this->p_libvlc->p_module_bank = p_bank;
  148.     vlc_object_attach( p_bank, p_this->p_libvlc );
  149.  
  150.     return;
  151. }
  152.  
  153. /*****************************************************************************
  154.  * module_ResetBank: reset the module bank.
  155.  *****************************************************************************
  156.  * This function resets the module bank by unloading all unused plugin
  157.  * modules.
  158.  *****************************************************************************/
  159. void __module_ResetBank( vlc_object_t *p_this )
  160. {
  161.     msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
  162.     return;
  163. }
  164.  
  165. /*****************************************************************************
  166.  * module_EndBank: empty the module bank.
  167.  *****************************************************************************
  168.  * This function unloads all unused plugin modules and empties the module
  169.  * bank in case of success.
  170.  *****************************************************************************/
  171. void __module_EndBank( vlc_object_t *p_this )
  172. {
  173.     module_t * p_next;
  174.  
  175.     vlc_object_detach( p_this->p_libvlc->p_module_bank );
  176.  
  177.     while( p_this->p_libvlc->p_module_bank->i_children )
  178.     {
  179.         p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
  180.  
  181.         if( DeleteModule( p_next ) )
  182.         {
  183.             /* Module deletion failed */
  184.             msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
  185.                      p_next->psz_object_name );
  186.  
  187.             /* We just free the module by hand. Niahahahahaha. */
  188.             vlc_object_detach( p_next );
  189.             vlc_object_destroy( p_next );
  190.         }
  191.     }
  192.  
  193.     vlc_object_destroy( p_this->p_libvlc->p_module_bank );
  194.  
  195.     return;
  196. }
  197.  
  198. /*****************************************************************************
  199.  * module_LoadMain: load the main program info into the module bank.
  200.  *****************************************************************************
  201.  * This function fills the module bank structure with the main module infos.
  202.  * This is very useful as it will allow us to consider the main program just
  203.  * as another module, and for instance the configuration options of main will
  204.  * be available in the module bank structure just as for every other module.
  205.  *****************************************************************************/
  206. void __module_LoadMain( vlc_object_t *p_this )
  207. {
  208.     AllocateBuiltinModule( p_this, vlc_entry__main );
  209. }
  210.  
  211. /*****************************************************************************
  212.  * module_LoadBuiltins: load all modules which we built with.
  213.  *****************************************************************************
  214.  * This function fills the module bank structure with the builtin modules.
  215.  *****************************************************************************/
  216. void __module_LoadBuiltins( vlc_object_t * p_this )
  217. {
  218.     msg_Dbg( p_this, "checking builtin modules" );
  219.     ALLOCATE_ALL_BUILTINS();
  220. }
  221.  
  222. /*****************************************************************************
  223.  * module_LoadPlugins: load all plugin modules we can find.
  224.  *****************************************************************************
  225.  * This function fills the module bank structure with the plugin modules.
  226.  *****************************************************************************/
  227. void __module_LoadPlugins( vlc_object_t * p_this )
  228. {
  229. #ifdef HAVE_DYNAMIC_PLUGINS
  230.     msg_Dbg( p_this, "checking plugin modules" );
  231.     AllocateAllPlugins( p_this );
  232. #endif
  233. }
  234.  
  235. /*****************************************************************************
  236.  * module_Need: return the best module function, given a capability list.
  237.  *****************************************************************************
  238.  * This function returns the module that best fits the asked capabilities.
  239.  *****************************************************************************/
  240. module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
  241.                           const char *psz_name )
  242. {
  243.     typedef struct module_list_t module_list_t;
  244.  
  245.     struct module_list_t
  246.     {
  247.         module_t *p_module;
  248.         int i_score;
  249.         module_list_t *p_next;
  250.     };
  251.  
  252.     module_list_t *p_list, *p_first, *p_tmp;
  253.     vlc_list_t *p_all;
  254.  
  255.     int i_which_module, i_index = 0;
  256.     vlc_bool_t b_intf = VLC_FALSE;
  257.  
  258.     module_t *p_module;
  259.  
  260.     int   i_shortcuts = 0;
  261.     char *psz_shortcuts = NULL, *psz_var = NULL;
  262.  
  263.     msg_Dbg( p_this, "looking for %s module", psz_capability );
  264.  
  265.     /* Deal with variables */
  266.     if( psz_name && psz_name[0] == '$' )
  267.     {
  268.         psz_var = config_GetPsz( p_this, psz_name + 1 );
  269.         psz_name = psz_var;
  270.     }
  271.  
  272.     /* Count how many different shortcuts were asked for */
  273.     if( psz_name && *psz_name )
  274.     {
  275.         char *psz_parser;
  276.  
  277.         /* If the user wants none, give him none. */
  278.         if( !strcmp( psz_name, "none" ) )
  279.         {
  280.             if( psz_var ) free( psz_var );
  281.             return NULL;
  282.         }
  283.  
  284.         i_shortcuts++;
  285.         psz_shortcuts = strdup( psz_name );
  286.  
  287.         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
  288.         {
  289.             if( *psz_parser == ',' )
  290.             {
  291.                  *psz_parser = '\0';
  292.                  i_shortcuts++;
  293.             }
  294.         }
  295.     }
  296.  
  297.     /* Sort the modules and test them */
  298.     p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
  299.     p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
  300.     p_first = NULL;
  301.  
  302.     /* Parse the module list for capabilities and probe each of them */
  303.     for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
  304.     {
  305.         module_t * p_submodule = NULL;
  306.         int i_shortcut_bonus = 0, i_submodule;
  307.  
  308.         p_module = (module_t *)p_all->p_values[i_which_module].p_object;
  309.  
  310.         /* Test that this module can do what we need */
  311.         if( strcmp( p_module->psz_capability, psz_capability ) )
  312.         {
  313.             for( i_submodule = 0;
  314.                  i_submodule < p_module->i_children;
  315.                  i_submodule++ )
  316.             {
  317.                 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
  318.                                            ->psz_capability, psz_capability ) )
  319.                 {
  320.                     p_submodule =
  321.                             (module_t*)p_module->pp_children[ i_submodule ];
  322.                     break;
  323.                 }
  324.             }
  325.  
  326.             if( p_submodule == NULL )
  327.             {
  328.                 continue;
  329.             }
  330.  
  331.             p_module = p_submodule;
  332.         }
  333.  
  334.         /* Test if we have the required CPU */
  335.         if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
  336.         {
  337.             continue;
  338.         }
  339.  
  340.         /* If we required a shortcut, check this plugin provides it. */
  341.         if( i_shortcuts )
  342.         {
  343.             vlc_bool_t b_trash;
  344.             int i_dummy, i_short = i_shortcuts;
  345.             char *psz_name = psz_shortcuts;
  346.  
  347.             /* Let's drop modules with a 0 score (unless they are
  348.              * explicitly requested) */
  349.             b_trash = !p_module->i_score;
  350.  
  351.             while( i_short )
  352.             {
  353.                 /* If the last given shortcut is "none" and we couldn't
  354.                  * find the module in the list of provided shortcuts,
  355.                  * then kick the bastard out of here!!! */
  356.                 if( (i_short == 1) && !strcmp(psz_name, "none") )
  357.                 {
  358.                     b_trash = VLC_TRUE;
  359.                     break;
  360.                 }
  361.  
  362.                 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
  363.                 {
  364.                     if( !strcmp( psz_name,
  365.                                  p_module->pp_shortcuts[i_dummy] ) )
  366.                     {
  367.                         /* Found it */
  368.                         b_trash = VLC_FALSE;
  369.                         i_shortcut_bonus = i_short * 10000;
  370.                         break;
  371.                     }
  372.                 }
  373.  
  374.                 if( i_shortcut_bonus )
  375.                 {
  376.                     /* We found it... remember ? */
  377.                     break;
  378.                 }
  379.  
  380.                 /* Go to the next shortcut... This is so lame! */
  381.                 while( *psz_name )
  382.                 {
  383.                     psz_name++;
  384.                 }
  385.                 psz_name++;
  386.                 i_short--;
  387.             }
  388.  
  389.             if( b_trash )
  390.             {
  391.                 continue;
  392.             }
  393.         }
  394.         /* If we didn't require a shortcut, trash zero-scored plugins */
  395.         else if( !p_module->i_score )
  396.         {
  397.             continue;
  398.         }
  399.  
  400.         /* Special case: test if we requested a particular intf plugin */
  401.         if( p_module->psz_program
  402.              && !strcmp( p_module->psz_program,
  403.                          p_this->p_vlc->psz_object_name ) )
  404.         {
  405.             if( !b_intf )
  406.             {
  407.                 /* Remove previous non-matching plugins */
  408.                 i_index = 0;
  409.                 b_intf = VLC_TRUE;
  410.             }
  411.         }
  412.         else if( b_intf )
  413.         {
  414.             /* This one doesn't match */
  415.             continue;
  416.         }
  417.  
  418.         /* Store this new module */
  419.         p_list[ i_index ].p_module = p_module;
  420.         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
  421.  
  422.         /* Add it to the modules-to-probe list */
  423.         if( i_index == 0 )
  424.         {
  425.             p_list[ 0 ].p_next = NULL;
  426.             p_first = p_list;
  427.         }
  428.         else
  429.         {
  430.             /* Ok, so at school you learned that quicksort is quick, and
  431.              * bubble sort sucks raw eggs. But that's when dealing with
  432.              * thousands of items. Here we have barely 50. */
  433.             module_list_t *p_newlist = p_first;
  434.  
  435.             if( p_first->i_score < p_list[ i_index ].i_score )
  436.             {
  437.                 p_list[ i_index ].p_next = p_first;
  438.                 p_first = &p_list[ i_index ];
  439.             }
  440.             else
  441.             {
  442.                 while( p_newlist->p_next != NULL &&
  443.                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
  444.                 {
  445.                     p_newlist = p_newlist->p_next;
  446.                 }
  447.  
  448.                 p_list[ i_index ].p_next = p_newlist->p_next;
  449.                 p_newlist->p_next = &p_list[ i_index ];
  450.             }
  451.         }
  452.  
  453.         i_index++;
  454.     }
  455.  
  456.     msg_Dbg( p_this, "probing %i candidate%s",
  457.                      i_index, i_index == 1 ? "" : "s" );
  458.  
  459.     /* Lock all candidate modules */
  460.     p_tmp = p_first;
  461.     while( p_tmp != NULL )
  462.     {
  463.         vlc_object_yield( p_tmp->p_module );
  464.         p_tmp = p_tmp->p_next;
  465.     }
  466.  
  467.     /* We can release the list, interesting modules were yielded */
  468.     vlc_list_release( p_all );
  469.  
  470.     /* Parse the linked list and use the first successful module */
  471.     p_tmp = p_first;
  472.     while( p_tmp != NULL )
  473.     {
  474.         if( p_tmp->p_module->pf_activate
  475.              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
  476.         {
  477.             break;
  478.         }
  479.  
  480.         vlc_object_release( p_tmp->p_module );
  481.         p_tmp = p_tmp->p_next;
  482.     }
  483.  
  484.     /* Store the locked module value */
  485.     if( p_tmp != NULL )
  486.     {
  487.         p_module = p_tmp->p_module;
  488.         p_tmp = p_tmp->p_next;
  489.     }
  490.     else
  491.     {
  492.         p_module = NULL;
  493.     }
  494.  
  495.     /* Unlock the remaining modules */
  496.     while( p_tmp != NULL )
  497.     {
  498.         vlc_object_release( p_tmp->p_module );
  499.         p_tmp = p_tmp->p_next;
  500.     }
  501.  
  502.     free( p_list );
  503.  
  504.     if( p_module != NULL )
  505.     {
  506.         msg_Dbg( p_module, "using %s module \"%s\"",
  507.                  psz_capability, p_module->psz_object_name );
  508.     }
  509.     else if( p_first == NULL )
  510.     {
  511.         msg_Err( p_this, "no %s module matched \"%s\"",
  512.                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
  513.     }
  514.     else if( psz_name != NULL && *psz_name )
  515.     {
  516.         msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
  517.                   psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
  518.     }
  519.  
  520.     if( psz_shortcuts )
  521.     {
  522.         free( psz_shortcuts );
  523.     }
  524.  
  525.     if( psz_var )
  526.     {
  527.         free( psz_var );
  528.     }
  529.  
  530.     /* Don't forget that the module is still locked */
  531.     return p_module;
  532. }
  533.  
  534. /*****************************************************************************
  535.  * module_Unneed: decrease the usage count of a module.
  536.  *****************************************************************************
  537.  * This function must be called by the thread that called module_Need, to
  538.  * decrease the reference count and allow for hiding of modules.
  539.  *****************************************************************************/
  540. void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
  541. {
  542.     /* Use the close method */
  543.     if( p_module->pf_deactivate )
  544.     {
  545.         p_module->pf_deactivate( p_this );
  546.     }
  547.  
  548.     msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
  549.  
  550.     vlc_object_release( p_module );
  551.  
  552.     return;
  553. }
  554.  
  555. /*****************************************************************************
  556.  * Following functions are local.
  557.  *****************************************************************************/
  558.  
  559. /*****************************************************************************
  560.  * AllocateAllPlugins: load all plugin modules we can find.
  561.  *****************************************************************************/
  562. #ifdef HAVE_DYNAMIC_PLUGINS
  563. static void AllocateAllPlugins( vlc_object_t *p_this )
  564. {
  565.     /* Yes, there are two NULLs because we replace one with "plugin-path". */
  566.     char *          path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
  567.                                NULL };
  568.  
  569.     char **         ppsz_path = path;
  570.     char *          psz_fullpath;
  571. #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
  572.      || ( defined( WIN32 ) && !defined( UNDER_CE ) )
  573.     int             i_vlclen = strlen( p_this->p_libvlc->psz_vlcpath );
  574.     vlc_bool_t      b_notinroot;
  575. #endif
  576.  
  577. #if defined( UNDER_CE )
  578.     wchar_t         psz_dir[MAX_PATH];
  579. #endif
  580.  
  581.     /* If the user provided a plugin path, we add it to the list */
  582.     path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
  583.                                                             "plugin-path" );
  584.  
  585. #if defined( WIN32 ) && !defined( UNDER_CE ) && !defined( _MSC_VER )
  586.     /* If there is no 'plugins' nor 'modules' subdirectory, the user may have
  587.      * screwed up the unzipping stage, so we look into '.' instead */
  588.     if( !opendir( "plugins" ) && !opendir( "modules" )
  589.         && !strcmp( *ppsz_path, "modules" ) )
  590.     {
  591.         *ppsz_path = ".";
  592.     }
  593. #endif
  594.  
  595.     for( ; *ppsz_path != NULL ; ppsz_path++ )
  596.     {
  597. #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
  598.      || ( defined( WIN32 ) && !defined( UNDER_CE ) )
  599.         /* Store strlen(*ppsz_path) for later use. */
  600.         int i_dirlen = strlen( *ppsz_path );
  601.  
  602.         b_notinroot = VLC_FALSE;
  603.         /* Under BeOS, we need to add beos_GetProgramPath() to access
  604.          * files under the current directory */
  605. #ifdef WIN32
  606.         if( i_dirlen < 3 || (*ppsz_path)[3] != '\\' )
  607. #else
  608.         if( (*ppsz_path)[0] != '/' )
  609. #endif
  610.         {
  611.             i_dirlen += i_vlclen + 2;
  612.             b_notinroot = VLC_TRUE;
  613.  
  614.             psz_fullpath = malloc( i_dirlen );
  615.             if( psz_fullpath == NULL )
  616.             {
  617.                 continue;
  618.             }
  619. #ifdef WIN32
  620.             sprintf( psz_fullpath, "%s\\%s",
  621.                      p_this->p_libvlc->psz_vlcpath, *ppsz_path );
  622. #else
  623.             sprintf( psz_fullpath, "%s/%s",
  624.                      p_this->p_libvlc->psz_vlcpath, *ppsz_path );
  625. #endif
  626.         }
  627.         else
  628. #endif
  629.         {
  630.             psz_fullpath = *ppsz_path;
  631.         }
  632.  
  633.         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
  634.  
  635.         /* Don't go deeper than 5 subdirectories */
  636. #if defined( UNDER_CE )
  637.         MultiByteToWideChar( CP_ACP, 0, psz_fullpath, -1, psz_dir, MAX_PATH );
  638.         AllocatePluginDir( p_this, psz_dir, 5 );
  639. #else
  640.         AllocatePluginDir( p_this, psz_fullpath, 5 );
  641. #endif
  642.  
  643. #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
  644.         if( b_notinroot )
  645.         {
  646.             free( psz_fullpath );
  647.         }
  648. #endif
  649.     }
  650.  
  651.     /* Free plugin-path */
  652.     free( path[ sizeof(path)/sizeof(char*) - 2 ] );
  653.     path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
  654. }
  655.  
  656. /*****************************************************************************
  657.  * AllocatePluginDir: recursively parse a directory to look for plugins
  658.  *****************************************************************************/
  659. static void AllocatePluginDir( vlc_object_t *p_this, const MYCHAR *psz_dir,
  660.                                int i_maxdepth )
  661. {
  662. #if defined( UNDER_CE ) || defined( _MSC_VER )
  663. #ifdef UNDER_CE
  664.     MYCHAR psz_path[MAX_PATH + 256];
  665. #else
  666.     char psz_path[MAX_PATH + 256];
  667. #endif
  668.     WIN32_FIND_DATA finddata;
  669.     HANDLE handle;
  670.     unsigned int rc;
  671. #else
  672.     int    i_dirlen;
  673.     DIR *  dir;
  674.     char * psz_file;
  675.     struct dirent * file;
  676. #endif
  677.  
  678.     if( i_maxdepth < 0 )
  679.     {
  680.         return;
  681.     }
  682.  
  683. #if defined( UNDER_CE ) || defined( _MSC_VER )
  684.     rc = GetFileAttributes( psz_dir );
  685.     if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
  686.     {
  687.         /* Not a directory */
  688.         return;
  689.     }
  690.  
  691.     /* Parse all files in the directory */
  692. #ifdef UNDER_CE
  693.     swprintf( psz_path, L"%s\\*.*", psz_dir );
  694. #else
  695.     sprintf( psz_path, "%s\\*.*", psz_dir );
  696. #endif
  697.     handle = FindFirstFile( psz_path, &finddata );
  698.     if( handle == INVALID_HANDLE_VALUE )
  699.     {
  700.         /* Empty directory */
  701.         return;
  702.     }
  703.  
  704.     /* Parse the directory and try to load all files it contains. */
  705.     do
  706.     {
  707. #ifdef UNDER_CE
  708.         unsigned int i_len = wcslen( finddata.cFileName );
  709.         swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
  710. #else
  711.         unsigned int i_len = strlen( finddata.cFileName );
  712.         /* Skip ".", ".." and anything starting with "." */
  713.         if( !*finddata.cFileName || *finddata.cFileName == '.' )
  714.         {
  715.             if( !FindNextFile( handle, &finddata ) ) break;
  716.             continue;
  717.         }
  718.         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
  719. #endif
  720.  
  721.         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
  722.         {
  723.             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
  724.         }
  725.         else if( i_len > strlen( LIBEXT )
  726. #ifdef UNDER_CE
  727.                 )
  728. #else
  729.                   /* We only load files ending with LIBEXT */
  730.                   && !strncasecmp( psz_path + strlen( psz_path)
  731.                                    - strlen( LIBEXT ),
  732.                                    LIBEXT, strlen( LIBEXT ) ) )
  733. #endif
  734.         {
  735.             AllocatePluginFile( p_this, psz_path );
  736.         }
  737.     }
  738.     while( FindNextFile( handle, &finddata ) );
  739.  
  740.     /* Close the directory */
  741.     FindClose( handle );
  742.  
  743. #else
  744.     dir = opendir( psz_dir );
  745.     if( !dir )
  746.     {
  747.         return;
  748.     }
  749.  
  750.     i_dirlen = strlen( psz_dir );
  751.  
  752.     /* Parse the directory and try to load all files it contains. */
  753.     while( (file = readdir( dir )) )
  754.     {
  755.         struct stat statbuf;
  756.         unsigned int i_len;
  757.  
  758.         /* Skip ".", ".." and anything starting with "." */
  759.         if( !*file->d_name || *file->d_name == '.' )
  760.         {
  761.             continue;
  762.         }
  763.  
  764.         i_len = strlen( file->d_name );
  765.         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
  766. #ifdef WIN32
  767.         sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
  768. #else
  769.         sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
  770. #endif
  771.  
  772.         if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
  773.         {
  774.             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
  775.         }
  776.         else if( i_len > strlen( LIBEXT )
  777.                   /* We only load files ending with LIBEXT */
  778.                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
  779.                                    LIBEXT, strlen( LIBEXT ) ) )
  780.         {
  781.             AllocatePluginFile( p_this, psz_file );
  782.         }
  783.  
  784.         free( psz_file );
  785.     }
  786.  
  787.     /* Close the directory */
  788.     closedir( dir );
  789.  
  790. #endif
  791. }
  792.  
  793. /*****************************************************************************
  794.  * AllocatePluginFile: load a module into memory and initialize it.
  795.  *****************************************************************************
  796.  * This function loads a dynamically loadable module and allocates a structure
  797.  * for its information data. The module can then be handled by module_Need
  798.  * and module_Unneed. It can be removed by DeleteModule.
  799.  *****************************************************************************/
  800. static int AllocatePluginFile( vlc_object_t * p_this, MYCHAR * psz_file )
  801. {
  802.     module_t * p_module;
  803.     module_handle_t handle;
  804.  
  805. #ifdef UNDER_CE
  806.     char psz_filename[MAX_PATH];
  807.     WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_file, -1,
  808.                          psz_filename, MAX_PATH, NULL, NULL );
  809. #else
  810.     char * psz_filename = psz_file;
  811. #endif
  812.  
  813.     /* Try to dynamically load the module. */
  814.     if( module_load( psz_file, &handle ) )
  815.     {
  816.         char psz_buffer[256];
  817.  
  818.         /* The plugin module couldn't be opened */
  819.         msg_Warn( p_this, "cannot open `%s' (%s)",
  820.                   psz_filename, module_error( psz_buffer ) );
  821.         return -1;
  822.     }
  823.  
  824.     /* Now that we have successfully loaded the module, we can
  825.      * allocate a structure for it */
  826.     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
  827.     if( p_module == NULL )
  828.     {
  829.         msg_Err( p_this, "out of memory" );
  830.         module_unload( handle );
  831.         return -1;
  832.     }
  833.  
  834.     /* We need to fill these since they may be needed by CallEntry() */
  835.     p_module->psz_filename = psz_filename;
  836.     p_module->handle = handle;
  837.     p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
  838.  
  839.     /* Initialize the module: fill p_module->psz_object_name, default config */
  840.     if( CallEntry( p_module ) != 0 )
  841.     {
  842.         /* We couldn't call module_init() */
  843.         vlc_object_destroy( p_module );
  844.         module_unload( handle );
  845.         return -1;
  846.     }
  847.  
  848.     DupModule( p_module );
  849.     p_module->psz_filename = strdup( p_module->psz_filename );
  850.     p_module->psz_longname = strdup( p_module->psz_longname );
  851.  
  852.     /* Everything worked fine ! The module is ready to be added to the list. */
  853.     p_module->b_builtin = VLC_FALSE;
  854.  
  855.     /* msg_Dbg( p_this, "plugin \"%s\", %s",
  856.                 p_module->psz_object_name, p_module->psz_longname ); */
  857.  
  858.     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
  859.  
  860.     return 0;
  861. }
  862.  
  863. /*****************************************************************************
  864.  * DupModule: make a plugin module standalone.
  865.  *****************************************************************************
  866.  * This function duplicates all strings in the module, so that the dynamic
  867.  * object can be unloaded. It acts recursively on submodules.
  868.  *****************************************************************************/
  869. static void DupModule( module_t *p_module )
  870. {
  871.     char **pp_shortcut;
  872.     int i_submodule;
  873.  
  874.     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
  875.     {
  876.         *pp_shortcut = strdup( *pp_shortcut );
  877.     }
  878.  
  879.     /* We strdup() these entries so that they are still valid when the
  880.      * module is unloaded. */
  881.     p_module->psz_object_name = strdup( p_module->psz_object_name );
  882.     p_module->psz_capability = strdup( p_module->psz_capability );
  883.  
  884.     if( p_module->psz_program != NULL )
  885.     {
  886.         p_module->psz_program = strdup( p_module->psz_program );
  887.     }
  888.  
  889.     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
  890.     {
  891.         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
  892.     }
  893. }
  894.  
  895. /*****************************************************************************
  896.  * UndupModule: free a duplicated module.
  897.  *****************************************************************************
  898.  * This function frees the allocations done in DupModule().
  899.  *****************************************************************************/
  900. static void UndupModule( module_t *p_module )
  901. {
  902.     char **pp_shortcut;
  903.     int i_submodule;
  904.  
  905.     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
  906.     {
  907.         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
  908.     }
  909.  
  910.     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
  911.     {
  912.         free( *pp_shortcut );
  913.     }
  914.  
  915.     free( p_module->psz_object_name );
  916.     free( p_module->psz_capability );
  917.  
  918.     if( p_module->psz_program != NULL )
  919.     {
  920.         free( p_module->psz_program );
  921.     }
  922. }
  923.  
  924. #endif /* HAVE_DYNAMIC_PLUGINS */
  925.  
  926. /*****************************************************************************
  927.  * AllocateBuiltinModule: initialize a builtin module.
  928.  *****************************************************************************
  929.  * This function registers a builtin module and allocates a structure
  930.  * for its information data. The module can then be handled by module_Need
  931.  * and module_Unneed. It can be removed by DeleteModule.
  932.  *****************************************************************************/
  933. static int AllocateBuiltinModule( vlc_object_t * p_this,
  934.                                   int ( *pf_entry ) ( module_t * ) )
  935. {
  936.     module_t * p_module;
  937.  
  938.     /* Now that we have successfully loaded the module, we can
  939.      * allocate a structure for it */
  940.     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
  941.     if( p_module == NULL )
  942.     {
  943.         msg_Err( p_this, "out of memory" );
  944.         return -1;
  945.     }
  946.  
  947.     /* Initialize the module : fill p_module->psz_object_name, etc. */
  948.     if( pf_entry( p_module ) != 0 )
  949.     {
  950.         /* With a well-written module we shouldn't have to print an
  951.          * additional error message here, but just make sure. */
  952.         msg_Err( p_this, "failed calling entry point in builtin module" );
  953.         vlc_object_destroy( p_module );
  954.         return -1;
  955.     }
  956.  
  957.     /* Everything worked fine ! The module is ready to be added to the list. */
  958.     p_module->b_builtin = VLC_TRUE;
  959.  
  960.     /* msg_Dbg( p_this, "builtin \"%s\", %s",
  961.                 p_module->psz_object_name, p_module->psz_longname ); */
  962.  
  963.     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
  964.  
  965.     return 0;
  966. }
  967.  
  968. /*****************************************************************************
  969.  * DeleteModule: delete a module and its structure.
  970.  *****************************************************************************
  971.  * This function can only be called if the module isn't being used.
  972.  *****************************************************************************/
  973. static int DeleteModule( module_t * p_module )
  974. {
  975.     vlc_object_detach( p_module );
  976.  
  977.     /* We free the structures that we strdup()ed in Allocate*Module(). */
  978. #ifdef HAVE_DYNAMIC_PLUGINS
  979.     if( !p_module->b_builtin )
  980.     {
  981.         if( p_module->b_unloadable )
  982.         {
  983.             module_unload( p_module->handle );
  984.         }
  985.         UndupModule( p_module );
  986.         free( p_module->psz_filename );
  987.         free( p_module->psz_longname );
  988.     }
  989. #endif
  990.  
  991.     /* Free and detach the object's children */
  992.     while( p_module->i_children )
  993.     {
  994.         vlc_object_t *p_this = p_module->pp_children[0];
  995.         vlc_object_detach( p_this );
  996.         vlc_object_destroy( p_this );
  997.     }
  998.  
  999.     config_Free( p_module );
  1000.     vlc_object_destroy( p_module );
  1001.  
  1002.     return 0;
  1003. }
  1004.  
  1005. #ifdef HAVE_DYNAMIC_PLUGINS
  1006. /*****************************************************************************
  1007.  * CallEntry: call an entry point.
  1008.  *****************************************************************************
  1009.  * This function calls a symbol given its name and a module structure. The
  1010.  * symbol MUST refer to a function returning int and taking a module_t* as
  1011.  * an argument.
  1012.  *****************************************************************************/
  1013. static int CallEntry( module_t * p_module )
  1014. {
  1015.     static char *psz_name = "vlc_entry" MODULE_SUFFIX;
  1016.     int (* pf_symbol) ( module_t * p_module );
  1017.  
  1018.     /* Try to resolve the symbol */
  1019.     pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle,
  1020.                                                         psz_name );
  1021.  
  1022.     if( pf_symbol == NULL )
  1023.     {
  1024.         char psz_buffer[256];
  1025.  
  1026.         /* We couldn't load the symbol */
  1027.         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
  1028.                             psz_name, p_module->psz_filename,
  1029.                             module_error( psz_buffer ) );
  1030.         return -1;
  1031.     }
  1032.  
  1033.     /* We can now try to call the symbol */
  1034.     if( pf_symbol( p_module ) != 0 )
  1035.     {
  1036.         /* With a well-written module we shouldn't have to print an
  1037.          * additional error message here, but just make sure. */
  1038.         msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
  1039.                            psz_name, p_module->psz_filename );
  1040.         return -1;
  1041.     }
  1042.  
  1043.     /* Everything worked fine, we can return */
  1044.     return 0;
  1045. }
  1046. #endif /* HAVE_DYNAMIC_PLUGINS */
  1047.